package com.jiangli.controller.admin;


import com.jiangli.annotation.Log;
import com.jiangli.dto.IngredientDTO;
import com.jiangli.dto.IngredientPageQueryDTO;
import com.jiangli.result.PageResult;
import com.jiangli.result.Result;
import com.jiangli.service.AdminIngredientService;
import com.jiangli.vo.IngredientVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/ingredient")
@Slf4j
@Api(tags = "管理端材料接口")
public class IngredientController {


    private final AdminIngredientService adminIngredientService;
    @Autowired
    public IngredientController(AdminIngredientService adminIngredientService) {
        this.adminIngredientService = adminIngredientService;
    }
    @GetMapping
    @ApiOperation("获取材料列表")
    public Result<PageResult> getIngredientByPage(IngredientPageQueryDTO ingredientPageQueryDTO) {
        PageResult pageResult = adminIngredientService.getIngredientByPage(ingredientPageQueryDTO);
        return Result.success(pageResult);
    }
    @PostMapping
    @ApiOperation("添加材料")
    @Log("添加材料")
    public Result<String> addIngredient(@RequestBody IngredientDTO ingredientDTO) {
        try {
            adminIngredientService.addIngredient(ingredientDTO);
            return Result.success("添加成功");
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    @PutMapping
    @ApiOperation("修改材料")
    @Log("修改材料")
    public Result<String> updateIngredient(@RequestBody IngredientDTO ingredientDTO) {
        try {
            adminIngredientService.updateIngredient(ingredientDTO);
            return Result.success("修改成功");
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    @GetMapping("/detail")
    @ApiOperation("材料详情")
    public Result<IngredientVO> getIngredientById(@RequestParam(required = true) Long id) {
        IngredientVO ingredientVO = adminIngredientService.getIngredientDetail(id);
        return Result.success(ingredientVO);
    }
}
